package com.qipay.card.service.impl;

import com.qipay.baas.model.CardBind;
import com.qipay.card.service.CardBindService;
import com.qipay.mapper.CardBindMapper;
import io.jsonwebtoken.lang.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Creator: Yao
 * Date:    2017/10/18
 * For:
 * Other:
 */
@Service
public class CardBindServiceImpl implements CardBindService {

    @Autowired
    CardBindMapper mapper;


    @Override
    public List<CardBind> listByUserId(Long userId) {

        // check
        Assert.notNull(userId);

        return mapper.listByUserId(userId);
    }

    @Override
    @Transactional
    public CardBind save(CardBind card) {

        Assert.notNull(card);

        if (null == card.getId() || 0 == card.getId()) {

            mapper.insert(card);
        } else {

            mapper.updateByPrimaryKey(card);
        }

        return card;
    }

    @Override
    public CardBind get(Long cardBindId) {

        // check
        Assert.notNull(cardBindId);

        return mapper.selectByPrimaryKey(cardBindId);
    }

    @Override
    public long count(Integer userId) {

        // check
        Assert.notNull(userId);

        return mapper.total(userId);
    }

    @Override
    public List<CardBind> page(Integer userId, Integer page, Integer size) {

        // check
        Assert.notNull(userId);
        Assert.notNull(page);
        Assert.notNull(size);

        // init
        int start = (null == page || 0 == page) ? 0 : (page - 1) * size;
        int offset = (null == size || 0 == size) ? 10 : size;

        return mapper.pageByUserId(userId, start, offset);
    }

    @Override
    @Transactional
    public void delete(Long bindId) {

        // check
        Assert.notNull(bindId);

        mapper.deleteByPrimaryKey(bindId);
    }
}
